import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class demo2 {
    public static void main(String[] args) throws SQLException {
        //1.创建dataSource
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/score?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");
        //2.建立连接
        Connection connection = dataSource.getConnection();
        //3.构造sql
        String sql = "select * from exam_result";
        PreparedStatement statement = connection.prepareStatement(sql);
        //4.使用sql
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()){
            String name = resultSet.getString("name");
            double chinese = resultSet.getInt("chinese");
            double math = resultSet.getInt("math");
            double english = resultSet.getInt("english");

            System.out.println(name+" "+chinese+" "+math+" "+english);

        }
        //5.关闭资源
        resultSet.close();
        statement.close();
        connection.close();

    }
}
